package com.webserver.servlet;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class LoginServlet {

    public void service(HttpRequest request, HttpResponse response){
        System.out.println("开始处理登录");

        String name = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(name+","+password);
        if (name == null || password == null){
            response.setEntity(new File(
                    "./webapps/myweb/login_fail.html"));
            System.out.println("有一个为空");
            return;
        }
        File file = new File("users/"+name+".obj");
        if (file.exists()){
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(file))
            ){
                User u = (User) ois.readObject();
                if (password.equals(u.getPasswrod())){
                    file = new File(
                            "./webapps/myweb/login_success.html");
                    response.setEntity(file);
                    System.out.println("成功______________________");
                }else {
                    file = new File(
                            "./webapps/myweb/login_fail.html");
                    response.setEntity(file);
                    System.out.println("密码错误>>>>>>>>>>>>>>>>>>>");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            file = new File("./webapps/myweb/login_fail.html");
            response.setEntity(file);
            System.out.println("用户不存在");
        }
        System.out.println("处理完毕");
    }
}
